//
//  QYBorrowedRecordViewController.m
//  QingYue
//
//  Created by Zheng on 2017/6/14.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYBorrowedRecordViewController.h"
#import "QYBorrowedRecordCell.h"
#import "QYBorrowedRecordModel.h"
#import "QYPublishCommentsViewController.h"

@interface QYBorrowedRecordViewController ()

@property (nonatomic, strong) NSMutableArray<QYBorrowedRecordModel *> *dataArr;

@end

@implementation QYBorrowedRecordViewController

- (instancetype)initWithStyle:(UITableViewStyle)style {
    self = [super initWithStyle:style];
    if (self) {
        [self loadData];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    self.title = @"借阅记录";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self.tableView registerClass:[QYBorrowedRecordCell class] forCellReuseIdentifier:@"QYBorrowedRecordCell"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - LoadData
- (void)loadData {
    [SVProgressHUD show];
    NSDictionary *param = @{@"username":[kUserDefaults objectForKey:@"username"]};
    [[YCRequestDataService sharedManager] Post:@"RecordBorrow" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            [SVProgressHUD dismissWithDelay:.35];
            _dataArr = [NSMutableArray new];
            NSArray<NSDictionary *> *borrowViewList = response[@"borrowViewList"];
            [borrowViewList enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                QYBorrowedRecordModel *model = [[QYBorrowedRecordModel alloc] initWithDataDic:obj];
                [_dataArr addObject:model];
            }];
            [self.tableView reloadData];
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

- (void)returnRequset:(NSNumber *)isbn {
    [SVProgressHUD show];
    NSDictionary *param = @{@"UserName":[kUserDefaults objectForKey:@"username"],
                            @"id":isbn};
    [[YCRequestDataService sharedManager] Post:@"RecordPreReturn" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            [SVProgressHUD dismissWithDelay:.35];
            [self loadData];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                QYPublishCommentsViewController *publishVC = [[QYPublishCommentsViewController alloc] init];
                publishVC.isbn = isbn;
                [self.navigationController pushViewController:publishVC animated:YES];
            });

        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

- (void)renewRequest:(NSNumber *)isbn {
    [SVProgressHUD show];
    NSDictionary *param = @{@"UserName":[kUserDefaults objectForKey:@"username"],
                            @"ProperthID":isbn};
    [[YCRequestDataService sharedManager] Post:@"ContinueBorrow" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            [SVProgressHUD dismissWithDelay:.35];
            [self loadData];
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

#pragma mark - TableviewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QYBorrowedRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QYBorrowedRecordCell" forIndexPath:indexPath];
    
    cell.returnBlock = ^(NSNumber *isbn) {
        [self returnRequset:isbn];
    };
    
    cell.renewBlock = ^(NSNumber *isbn) {
        [self renewRequest:isbn];
    };
    
    QYBorrowedRecordModel *model = _dataArr[indexPath.row];
    cell.model = model;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 240;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return .0001f;
}

@end
